package common.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLReader {
	public static Document readXMLFile(String data) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setCoalescing(true);
		factory.setIgnoringComments(true);
		factory.setIgnoringElementContentWhitespace(true);
		factory.setValidating(false);
		DocumentBuilder builder = factory.newDocumentBuilder();
		StringReader reader = new StringReader(data);
		InputSource inputSource = new InputSource(reader);
		Document doc = builder.parse(inputSource);
		return doc;
	}
	
	public static boolean validateDTD(String xmlFile) throws ParserConfigurationException, IOException{
		try {
		  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		  factory.setValidating(true);
		  factory.setNamespaceAware(true);
		
		  DocumentBuilder builder = factory.newDocumentBuilder();
		
		  builder.setErrorHandler(new MyErrorHandler());
	      builder.parse(new InputSource(xmlFile));
	      return true;
		}catch (ParserConfigurationException pce) {
		      throw pce;
		}catch (IOException io) {
		      throw io;
		}catch (SAXException se){
		      return false;
		}
	}
	
	public static boolean validateXSD(String xmlFileName, String xsdFileName) throws  ParserConfigurationException, IOException {
        try{
        	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        	// Disable the document validation as the document is being parsed.
        	factory.setValidating(false);
        	factory.setNamespaceAware(true);
        	
        	SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        	factory.setSchema(schemaFactory.newSchema(new StreamSource(new BufferedReader(new FileReader(xsdFileName)))));

        	DocumentBuilder builder = factory.newDocumentBuilder();
        	builder.setErrorHandler(new MyErrorHandler());

        	builder.parse(new InputSource(xmlFileName));
            return true;
        }catch (ParserConfigurationException pce) {
		      throw pce;
		}catch (IOException io) {
		      throw io;
        }catch(SAXException e){
        	return false;
        }
    }
	
	public static void DOMwriteXML(String filename, Document doc) throws Exception{
		File f = new File(filename);
		Source source = new DOMSource(doc);
		StreamResult result = new StreamResult(f);
		Transformer trans = TransformerFactory.newInstance().newTransformer();
		trans.setOutputProperty(OutputKeys.INDENT, "yes");
		trans.transform(source, result);
		
	}
}
